package com.ld.hpfs.utils;

import com.google.common.base.Function;
import com.google.common.collect.Lists;
import com.ld.hpfs.web.interseptor.WebConstants;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class BatchProcessHelper {
    public static List<HashMap<String, Object>> batchProcess(HashMap<String, Object> param,
                                                             Function<HashMap<String, Object>, List<HashMap<String, Object>>> operationFunc) {
        List<Map<String, String>> ids = (List<Map<String, String>>) param.get("list");
        List<HashMap<String, Object>> result = new ArrayList<HashMap<String, Object>>();

        List<List<Map<String, String>>> partitionIds = Lists.partition(ids, WebConstants.BATCH_SIZE);
        for (List<Map<String, String>> partitionId : partitionIds) {
            param.put("list", partitionId);
            List<HashMap<String, Object>> resultItem = operationFunc.apply(param);
            if (resultItem != null ) {
                result.addAll(resultItem);
            }
        }
        return result;
    }
}
